using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace QLVMB
{
    public partial class frmSuaChuyenBay : Form
    {
        public DTO.ChuyenBay cb = new DTO.ChuyenBay();
        DTO.ChiTietChuyenBay ctcb = new DTO.ChiTietChuyenBay();
        List<DTO.SanBay> lsb = new List<DTO.SanBay>();
        List<DTO.MayBay> lmb = new List<DTO.MayBay>();
        List<DTO.ChiTietChuyenBay> lctcb = new List<DTO.ChiTietChuyenBay>();
        DTO.ChuyenBay cbmoi = new DTO.ChuyenBay();


        public frmSuaChuyenBay()
        {
            InitializeComponent();
        }

        private void frmSuaChuyenBay_Load(object sender, EventArgs e)
        {
            loadcombo();
            
        }
        void loadcombo()
        {
            
            List<DTO.SanBay> lsb1 = new List<DTO.SanBay>();
            List<DTO.SanBay> lsbtg = new List<DTO.SanBay>();
            DTO.SanBay a = new DTO.SanBay();
            a.MaSanBay = 0;
            a.TenSanBay = "";
            lsbtg.Add(a);


            txtMaCB.Text = cb.MaChuyenBay;
            dateTimePicker1.Value = cb.NgayBay;
            cbGio.Text = cb.ThoiGian.Hour.ToString();
            cbPhut.Text = cb.ThoiGian.Minute.ToString();
            txtHangI.Text = cb.SoGheThuongGia.ToString();
            txtHangII.Text = cb.SoGhePhoThong.ToString();

            if (BUS.SanBayBUS.ChonHet(lsb) == BUS.SanBayBUSResult.OK)
            {
                //for (int i = 0; i < lsb.Count; i++)
                    cbSBDen.DataSource = lsb;
                    cbSBDen.DisplayMember = "TenSanBay";
                    cbSBDen.ValueMember = "MaSanBay";
                    
            }
            if (BUS.SanBayBUS.ChonHet(lsb1) == BUS.SanBayBUSResult.OK)
            {
                //for (int i = 0; i < lsb.Count; i++)
                cbSBDi.DataSource = lsb1;
                cbSBDi.DisplayMember = "TenSanBay";
                cbSBDi.ValueMember = "MaSanBay";

            }
            if (BUS.SanBayBUS.ChonHet(lsbtg) == BUS.SanBayBUSResult.OK)
            {
                //for (int i = 0; i < lsb.Count; i++)
                cbSBTrungGian.DataSource = lsbtg;
                cbSBTrungGian.DisplayMember = "TenSanBay";
                cbSBTrungGian.ValueMember = "MaSanBay";

            }
            if (BUS.MayBayBUS.ChonHetMayBay(lmb) == BUS.MayBayBUSResult.OK)
            {
                cbMayBay.DataSource = lmb;
                cbMayBay.DisplayMember = "TenLoaiMayBay";
                cbMayBay.ValueMember = "MaLoaiMayBay";
                
            }
            if (BUS.ChiTietChuyenBayBUS.ChonHetCTChuyenBay(lctcb) == BUS.ChiTietChuyenBayBUSResult.OK)
            {
                for (int i = 0; i < lctcb.Count; i++)
                {
                    if (lctcb[i].MaChuyenBay == cb.MaChuyenBay)
                    {
                        for (int j = 0; j < lsb.Count; j++)
                        {
                            if (lctcb[i].SanBayTrungGian == lsb[j].MaSanBay)
                            {
                                cbSBTrungGian.Text = lsb[j].TenSanBay;
                                cbGioDung.Text = lctcb[i].ThoiGianDung.Hour.ToString();
                                cbPhutDung.Text = lctcb[i].ThoiGianDung.Minute.ToString();
                            }
                        }
                    }
                }
            }
            for (int i = 0; i < lsb.Count; i++)
            {
                if (cb.MaSanBayDen == lsb[i].MaSanBay)
                    cbSBDen.Text = lsb[i].TenSanBay;
                if (cb.MaSanBayDi == lsb[i].MaSanBay)
                    cbSBDi.Text = lsb[i].TenSanBay;
            }
            for (int i = 0; i < lmb.Count ; i++)
            {
                if (cb.MaLoaiMayBay == lmb[i].MaLoaiMayBay)
                    cbMayBay.Text = lmb[i].TenLoaiMayBay;
            }
            
        }

        private void btLuu_Click(object sender, EventArgs e)
        {
            cbmoi.MaChuyenBay = cb.MaChuyenBay;
            cbmoi.SoGhePhoThong = int.Parse(txtHangII.Text);
            cbmoi.SoGheThuongGia = int.Parse(txtHangI.Text);
            cbmoi.MaSanBayDi = int.Parse(cbSBDi.SelectedValue.ToString());
            cbmoi.MaSanBayDen = int.Parse(cbSBDen.SelectedValue.ToString());
            cbmoi.MaLoaiMayBay = int.Parse(cbMayBay.SelectedValue.ToString());
            cbmoi.NgayBay = DateTime.Parse(dateTimePicker1.Value.ToString());
            cbmoi.ThoiGian = DateTime.Parse(cbGio.Text + ":" + cbPhut.Text);
            ctcb.MaChuyenBay = cb.MaChuyenBay;
            ctcb.SanBayTrungGian = int.Parse(cbSBTrungGian.SelectedValue.ToString());
            ctcb.ThoiGianDung = DateTime.Parse(cbGioDung.Text + ":" + cbPhutDung.Text);
           // DAO.ChuyenBayDAO.CapNhatChuyenBay(cbmoi);
            if (BUS.ChuyenBayBUS.CapNhatChuyenBay(cbmoi) == BUS.ChuyenBayBUSResult.OK && BUS.ChiTietChuyenBayBUS.CapNhatChuyenBay(ctcb) == BUS.ChiTietChuyenBayBUSResult.OK)
               MessageBox.Show("Cap nhat thanh cong");
           else
           {
               MessageBox.Show("Loi cap nhat", "Error");
           }
        }


        
    }
}